﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{ 
    /// <summary>
    /// Một công trình kiến trúc trong game
    /// </summary>
    public class hStructure: hVisibleGameEntity
    {
        private Boolean _isSelected = false;
        private Boolean _acceptMousePress = true;
        private int _currentpower;
        private int _maxpower;
        private int _energy;
        private Boolean _clicked=false;
        private string _name;
        private int[] _actions = new int[9];
        private string _controler="";
        private string[] _terrainType;
        private List<hActionButton> _lstAction;
        private bool _isDead = false;
        public bool IsDead 
        {
            get { return _isDead; }
            set { _isDead = value; }
        }
        private int _cost = 0;
        public int Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }

        private Texture2D _cursor_select;

        #region Properties

        public List<hActionButton> ActionList
        {
            get { return _lstAction; }
            set { _lstAction = value; }
        }

        private int _vision = 125;
        public int Vision
        {
            get { return _vision; }
            set { _vision = value; }
        }

        public string[] TerrainType
        {
            get { return _terrainType; }
            set { _terrainType = value; }
        }


        public string Controler
        {
            get { return _controler; }
            set { _controler = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int[] Actions
        {
            get { return _actions; }
            set { _actions = value; }
        }

        public Boolean Clicked
        {
            get { return _clicked; }
            set { _clicked = value; }
        }

        public int Power
        {
            get { return _currentpower; }
            set { _currentpower = value; }
        }

        public int MaxPower
        {
            get { return _maxpower; }
            set { _maxpower = value; }
        }

        public int Energy
        {
            get { return _energy; }
            set { _energy = value; }
        }

        public Boolean Selected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }

        #endregion

        //// dân trong công trình, chỉ để trang trí
        //hSprite _citizen;

        public hStructure()
        {

        }

        public hVisibleGameEntity clone(hParams args)
        {
            hVisibleGameEntity newObject = new hStructure();
            newObject._nsprite = this._nsprite;
            newObject._sprite = new hSprite[this._nsprite];
            for (int i = 0; i < this._nsprite; i++)
            {
                newObject._sprite[i] = new hSprite(this._sprite[i].Texture);
            }

            newObject.X = this.X;
            newObject.Y = this.Y;
            newObject.Width = this.Width;
            newObject.Height = this.Height;
            ((hStructure)newObject)._currentpower = this._currentpower;
            ((hStructure)newObject)._energy = this._energy;
            ((hStructure)newObject)._actions = this._actions;
            ((hStructure)newObject)._name = this._name;
            ((hStructure)newObject)._terrainType = this._terrainType;
            //((hStructure)newObject)._citizen = new hSprite(this._citizen.Texture);
            //((hStructure)newObject)._citizen.setPosition(X + Width/2, Y + Height/2);
            //((hStructure)newObject)._citizen.TempX = X;
            //((hStructure)newObject)._citizen.TempY = Y;
            ((hStructure)newObject).Controler = this.Controler;
            ((hStructure)newObject).TerrainType = this.TerrainType;
            ((hStructure)newObject)._cost = this._cost;
            if (this._actions != null)
            {
                ((hStructure)newObject)._lstAction = new List<hActionButton>();
                for (int i = 0; i < this._actions.Count(); i++)
                {
                    ((hStructure)newObject)._lstAction.Add(args.ActionManager.createObject(this._actions[i]));
                }
            }
            ((hStructure)newObject)._cursor_select = args.CommonTexture.CursorSelectStruct;
            return newObject;
        }

        public hStructure(ContentManager content, string[] argStateTexture, float posX, float PosY, hControlPanel ctrlP)
        {
            //Texture2D[] txt = new Texture2D[1];
            //txt[0] = content.Load<Texture2D>(hGlobal.PathSructure + "probe");
            //_citizen = new hSprite(txt);

            _nsprite = 1;
            _sprite = new hSprite[_nsprite];
            int argc = argStateTexture.Count();
            Texture2D[] texture = new Texture2D[argc];
            for (int i = 0; i < argc; i++)
            {
                texture[i] = content.Load<Texture2D>(argStateTexture[i]);
            }
            _sprite[0] = new hSprite(texture);
            Width = _sprite[0].Width;
            Height = _sprite[0].Height;
            X = _sprite[0].X;
            Y = _sprite[0].Y;

            for (int i = 0; i < 9; i++)
            {
                Actions[i] = -1;
            }
        }

        public int nDelay = 2;
        protected int iDelay = 0;

        public override void update(hParams args, GameTime gameTime)
        {
            if (Power <= 0)
            {
                IsDead = true;
                return;
            }

            //// cập nhật vị trí thường dân                
            //if (_citizen.TempX != X || _citizen.TempY != Y)
            //{
            //    _citizen.setPosition(_citizen.X + (X - _citizen.TempX), _citizen.Y + (Y - _citizen.TempY));
            //    _citizen.TempX = X;
            //    _citizen.TempY = Y;
            //}
            //if (iDelay == 0)
            //{
            //    _citizen.X += args.random.Next(-2, 3);
            //    _citizen.X = MathHelper.Clamp(_citizen.X, X + Width/10, X + 9*Width/10);
            //    _citizen.Y += args.random.Next(-2, 3);
            //    _citizen.Y = MathHelper.Clamp(_citizen.Y, Y + Height/10, Y + 9*Height/10);
            //    _citizen.RotationAngle += MathHelper.ToRadians(args.random.Next(0,2));
            //    if (_citizen.RotationAngle >= MathHelper.TwoPi)
            //        _citizen.RotationAngle = 0;
            //}
            //iDelay = (iDelay + 1) % nDelay;

            X += args.Grid.dX;
            Y += args.Grid.dY;

            MouseState mouse = Mouse.GetState();

            // không xử lý chuột nếu chuột trên control panel
            // hay đang trong pha chọn vị trí
            if (args.IsMouseOverControlPanel || args.ControlPhase == hGlobal.ControlPhase.Selecting)
            {
                return;
            }

            if (mouse.LeftButton == ButtonState.Pressed && _acceptMousePress)
            {
                _acceptMousePress = false;

                if (mouse.X > X && mouse.X < X + Width
                    && mouse.Y > Y && mouse.Y < Y + Height)
                {
                    Clicked = true;
                    if (Controler.Equals("player"))
                    {
                        args.ActionManager.setCurrentActionButtons(ActionList);
                        args.ActionManager.ActionCaller = this;
                    }
                    else
                    {
                        args.ActionManager.setCurrentActionButtons(null);
                    }
                }
                else
                {
                    Clicked = false;
                }
            } 

            if (mouse.LeftButton == ButtonState.Released)
            {
                _acceptMousePress = true;
            }
        }

        public new void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //if (this.Controler.Equals("player"))
            //{
            //    if (_citizen.Y < Y + Height / 5)
            //    {
            //        _citizen.draw(gameTime, spriteBatch, null, Color.White, new Vector2(_citizen.Width / 2, _citizen.Height / 2), SpriteEffects.None);
            //    }
            //}
            if (Selected)
            {
                if (Controler.Equals("player"))
                {
                    spriteBatch.Draw(_cursor_select, new Vector2(X, Y), Color.White);
                }
                else
                {
                    spriteBatch.Draw(_cursor_select, new Vector2(X, Y), Color.Red);
                }
            }

            if (!IsDead)
            {
                base.draw(gameTime, spriteBatch);
            }
            else
            {
                base.draw(gameTime, spriteBatch,100);
            }

            //if (this.Controler.Equals("player"))
            //{
            //    if ((_citizen.Y > Y + 4 * Height / 5 || _citizen.X < X + Width / 5 || _citizen.X > X + 4 * Width / 5) && (_citizen.Y >= Y + 2 * Height / 5))
            //    {
            //        _citizen.draw(gameTime, spriteBatch, null, Color.White, new Vector2(_citizen.Width / 2, _citizen.Height / 2), SpriteEffects.None);
            //    }
            //}
        }
    }
}
